// Copyright 2007 Google Inc. All Rights Reserved.

package com.google.jessewilson.codesearch.io.jdom;

import com.google.jessewilson.codesearch.util.Reflection;
import org.jdom.Element;

/**
 * Write or read an object from XML.
 *
 * @author jessewilson
 */
public interface Externalizer<T> {

  void writeExternal(Element target, T data);

  T readExternal(Element source);

  /**
   * Lookup an externalizer for a specified class.
   *
   * <p>By convention, all classes should have a static inner class named
   * MyExternalizer.
   */
  static class Finder {
    public static <T> Externalizer<T> find(String forType) {
      String externalizerName = forType + "$MyExternalizer";
      return Reflection.newInstance(externalizerName, null);
    }
    public static <T> Externalizer<T> find(Class<? extends T> forType) {
      return find(forType.getName());
    }
  }
}
